import { Component, OnInit } from "@angular/core";
import { NzNotificationService, UploadFile } from "ng-zorro-antd";
import {ActivatedRoute, Router} from "@angular/router";

import { NzModalService, NzModalSubject } from "ng-zorro-antd";
import {Config} from "../../../service/config";
import {HttpServcie} from "../../../service/http.service";
import {DetectionRecordBean} from "../detection-record.bean";

@Component({
  selector: "detection-record",
  templateUrl: "detection-record.html",
  styleUrls: ["detection-record.scss"]
})
export class DetectionRecord implements OnInit {
  detectionRecord : DetectionRecordBean = new DetectionRecordBean();
  modelTitle: string;
  modal: number;
  updateData: any;
  list: any = [];
  allChecked = false;
  indeterminate = false;
  currentModal: any;
  isConfirmLoading = false;
  isAddVisible = false;
  config = Config;
  total = 0;

  constructor(
    private httpServcie: HttpServcie,
    private router: Router,
    private _notification: NzNotificationService,
    private nsub: NzModalSubject,
    private modalService: NzModalService,
    private route: ActivatedRoute,
  ) {

  }

  ngOnInit() {
    this.route.paramMap.subscribe(data => {
      //查询电梯的信息
      let id = data.get("id");
      this.detectionRecord.planDetailId = id;
    });
    this.detectionRecord.pageSize = 10;
    this.detectionRecord.currentPage = 1;
  }

  query() {
    this.httpServcie.queryDetectionElevator(this.detectionRecord).subscribe(data=>{
      if(data.status == '200'){
        this.list = data.data.list;
        this.total = data.data.total;
      }else{
        this._notification.create("error", "错误提示", "查询失败！");
        return
      }
    });
  }

  /**
   * 跳转到检查报告详情
   * @param id
   */
  toDetail(param){
    this.router.navigate(['/home/detection-record',{planDetailId:param}]);
  }


  // 查询面板
  panels = {
    active: true,
    name: "查询",
    disabled: false,
    childPanel: [
      {
        active: false,
        name: "查询"
      }
    ]
  };

  //table加载数据时
  displayDataChange($event) {
    this.list = $event;
    this.refreshStatus();
  }

  //重置选择框
  refreshStatus() {
    const allChecked = this.list.every(
      value => value.disabled || value.checked
    );
    const allUnChecked = this.list.every(
      value => value.disabled || !value.checked
    );
    this.allChecked = allChecked;
    this.indeterminate = !allChecked && !allUnChecked;
  }

  //全选
  checkAll(value) {
    if (value) {
      this.list.forEach(data => {
        if (!data.disabled) {
          data.checked = true;
        }
      });
    } else {
      this.list.forEach(data => (data.checked = false));
    }
  }

  //分页
  nzPageSizeChange(event) {
    this.detectionRecord.pageSize = event;
    this.detectionRecord.currentPage = 1;
    this.query();
  }

  //输入框的方法
  nzPageIndexChange(event) {
    this.detectionRecord.currentPage = event;
    this.query();
  }

  //当前页
  nzPageIndexClickChange(event) {
    this.detectionRecord.currentPage = event;
    this.query();
  }


  //添加添加页面的数据
  handleOk(e) {
    this.isConfirmLoading = true;
    setTimeout(() => {
      /* destroy方法可以传入onOk或者onCancel。默认是onCancel */
      this.currentModal.destroy();
      this.isConfirmLoading = false;
      this.currentModal = null;
    }, 1000);
    // this.isAddConfirmLoading = true;
    // this.inValue = "aaaaa";
  }

  //关闭model框
  handleCancel = e => {
    console.log(e);
    this.isAddVisible = false;
  };

  //添加后返回的结果
  addOutPut(event) {
    this.isConfirmLoading = true;
    setTimeout(() => {
      /* destroy方法可以传入onOk或者onCancel。默认是onCancel */
      this.currentModal.destroy();
      this.isConfirmLoading = false;
      this.currentModal = null;
    }, 500);
    //this.planDict.currentPage = 1;
    this.query();
  }

  toBack() {
    window.history.back();
  }
}
